import axios from 'axios'
import {message} from 'ant-design-vue';

let baseURL = 'https://s2.vivivi.club'
// create an axios instance
const service = axios.create({
	baseURL: baseURL,
	withCredentials: false, // 不发送Cookie
	timeout: 50000 // request timeout
})
// request interceptor
service.interceptors.request.use(
	config => {
		// do something before request is sent
		config.url = config.url
		return config
	},
	error => {
		// do something with request error
		console.log('request-error', error) // for debug
		return Promise.reject(error)
	}
)

// response interceptor
service.interceptors.response.use(
	response => {
		const res = response.data
		// 200状态码表示正确；下面进行错误处理
		// console.log(222, res)
		if (res.code === 0) {
			message.error(res.msg);
		}
		return res
	},
	error => {
		message.info('系统错误！');
		return Promise.reject(error)
	}
)

const http = (method = 'get', url, param) => {
	let httpDefaultOpts = {
		method: method,
		url,
		params: param,
		data: param,
		headers: {
			'token': localStorage.getItem('token') || ''
		},
	}
	if (method === 'get') {
		delete httpDefaultOpts.data
	} else {
		delete httpDefaultOpts.params
	}
	// console.log('opt', httpDefaultOpts)
	return new Promise((resolve, reject) => {
		service(httpDefaultOpts).then(res => {
			resolve(res)
		}).catch(err => {
			reject(err)
		})
	})
}
export {
	http,
	baseURL
}
